package com.mjawor.lpgroovy

import com.mjawor.lpgroovy.vars.Var

class xVarImpl {
    private Var variable;
    private def coefficient;

    public xVarImpl(Var variable) {
        this.coefficient = 1
        this.variable = variable
    }

    public xVarImpl(coefficient, Var variable) {
        this.coefficient = coefficient
        this.variable = variable
    }

    Var getVariable() {
        return variable
    }

    def getCoefficient() {
        return coefficient
    }

    def plus(xVarImpl var) {
        if(var.variable.id == variable.id) {
            return new xVarImpl(coefficient + var.coefficient, variable.clone())
        } else {
            throw new RuntimeException("Trying to add coefficients of variables with different ids: "+var.variable.id+" and "+variable.id)
        }
    }

    @Override
    String toString() {
        return coefficient + "" + variable
    }

    @Override
    def clone() {

    }
}
